package com.example.iread.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.iread.R;
import com.example.iread.adapter.SearchHistoryAdapter;
import com.example.iread.application.MyApplication;
import com.example.iread.bean.SearchHistoryBean;
import com.example.iread.dao.SearchHistoryDao;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {

    private final String TAG = "SoRiya";

    private String keyword = "";

    SearchHistoryDao searchHistoryDao = MyApplication.getInstance().getDb().searchHistoryDao();
    private RecyclerView rv_search_history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        TextView btn_search_cancel = findViewById(R.id.btn_search_cancel);
        EditText et_search = findViewById(R.id.et_search);
        rv_search_history = findViewById(R.id.rv_search_history);

        btn_search_cancel.setOnClickListener(v -> {
            if (et_search.getText().toString().equals("")) {
                finish();
            } else {
                search(keyword);
            }
        });

        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}

            @SuppressLint("SetTextI18n")
            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                btn_search_cancel.setText("".contentEquals(charSequence) ? "Cancel" : "Go");
                keyword = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {}
        });

        et_search.setOnKeyListener(((view, keyCode, keyEvent) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP) {
                search(keyword);
                return true;
            }
            return false;
        }));

        List<SearchHistoryBean> list = searchHistoryDao.getList();
        TextView tv_search_clear_all = findViewById(R.id.tv_search_clear_all);

        tv_search_clear_all.setOnClickListener(v -> {
            searchHistoryDao.deleteAll();
            getSearchHistory(null);
            tv_search_clear_all.setVisibility(View.GONE);
        });

        if (list == null || list.size() == 0) tv_search_clear_all.setVisibility(View.GONE);

        getSearchHistory(list);
    }

    public void search(String keyword) {
        Intent intent = new Intent(this, SearchResultActivity.class);
        intent.putExtra("keyword", keyword);

        SearchHistoryBean searchHistoryBean = new SearchHistoryBean();
        searchHistoryBean.keyword = keyword;
        searchHistoryBean.createTime = System.currentTimeMillis();
        searchHistoryDao.insert(searchHistoryBean);

        startActivity(intent);
        finish();
    }

    public void getSearchHistory(List<SearchHistoryBean> list) {
        if (list == null) list = new ArrayList<>();
        rv_search_history.setAdapter(new SearchHistoryAdapter(this, list)); // 设置设配器
        rv_search_history.setLayoutManager(new LinearLayoutManager(this)); // 设置线性布局管理器
    }
}